package com.itheima.hystrix.web;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class RibbonController {
    
    @GetMapping("/order")
    //服务降级,超时降级,超时的同时也会把线程杀死,
    // 也就是 TimeUnit.MILLISECONDS.sleep(4000) 会抛异常,也就是说不算异步正常执行
    // @HystrixCommand(
    //         //回滚方法
    //         fallbackMethod = "fallback",
    //         //与此命令相关的属性,  由 @HystrixProperty 注解数组构成的
    //         commandProperties = {
    //                 @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    //         }
    // )
    
    //服务降级,调用的服务出问题,也会触发降级
    // @HystrixCommand(fallbackMethod = "fallback")
    
    //服务熔断注解
    // @HystrixCommand(
    //         //回滚方法
    //         fallbackMethod = "fallback",
    //         //线程池名称
    //         threadPoolKey = "hystrixPool",
    //         //与线程池相关的属性
    //         threadPoolProperties = {
    //                 //最大并发线程数
    //                 @HystrixProperty(name = "coreSize", value = "1"),
    //                 @HystrixProperty(name = "maxQueueSize", value = "-1"),
    //                 @HystrixProperty(name = "queueSizeRejectionThreshold", value = "0")
    //         },
    //         //与此命令相关的属性,  由 @HystrixProperty 注解数组构成的
    //         commandProperties = {
    //                 @HystrixProperty(name = "execution.timeout.enabled", value = "false")
    //         }
    // )
    @HystrixCommand(
            //回滚方法
            fallbackMethod = "fallback",
            //与此命令相关的属性,  由 @HystrixProperty 注解数组构成的
            commandProperties = {
                    @HystrixProperty(name = "execution.timeout.enabled", value = "false"),
                    @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE"),
                    @HystrixProperty(name = "execution.isolation.semaphore.maxConcurrentRequests", value = "1")
            }
    )
    public String order(@RequestParam(required = false, defaultValue = "itheima") String name) {
        log.info("开始调用积分服务...");
        try {
            TimeUnit.MILLISECONDS.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //订单服务调用积分服务，主动停掉积分(降级)
        String result = restTemplate.getForObject("http://nacos-client-hystrix/integral?name=" + name, String.class);
        log.info("调用积分服务完成");
        return result;
    }
    
    @Autowired
    RestTemplate restTemplate;
    
    /*
     * @Description:其他处理
     * @Method: fallback
     * @Param: [name]
     * @Update:
     * @since: 1.0.0
     * @Return: java.lang.String
     *
     */
    public String fallback(String name) {
        return "请稍后再试...";
    }
    
}
